/*     */ package com.zimbra.cs.mailbox.acl;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.util.memcached.MemcachedMap;
/*     */ import com.zimbra.common.util.memcached.MemcachedSerializer;
/*     */ import com.zimbra.common.util.memcached.ZimbraMemcachedClient;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.mailbox.ACL;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import com.zimbra.cs.mailbox.MetadataList;
/*     */ import com.zimbra.cs.memcached.MemcachedConnector;
/*     */ import com.zimbra.cs.session.PendingModifications;
/*     */ import com.zimbra.cs.session.PendingModifications.Change;
/*     */ import com.zimbra.cs.session.PendingModifications.ModificationKey;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class EffectiveACLCache
/*     */ {
/*  45 */   private static EffectiveACLCache sTheInstance = new EffectiveACLCache();
/*     */   
/*     */   private MemcachedMap<EffectiveACLCacheKey, ACL> mMemcachedLookup;
/*     */   
/*  49 */   public static EffectiveACLCache getInstance() { return sTheInstance; }
/*     */   
/*     */   EffectiveACLCache() {
/*  52 */     ZimbraMemcachedClient memcachedClient = MemcachedConnector.getClient();
/*  53 */     ACLSerializer serializer = new ACLSerializer();
/*  54 */     this.mMemcachedLookup = new MemcachedMap(memcachedClient, serializer);
/*     */   }
/*     */   
/*     */ 
/*     */   private static class ACLSerializer
/*     */     implements MemcachedSerializer<ACL>
/*     */   {
/*     */     public Object serialize(ACL value)
/*     */     {
/*  63 */       return value.encode().toString();
/*     */     }
/*     */     
/*     */     public ACL deserialize(Object obj) throws ServiceException
/*     */     {
/*     */       try
/*     */       {
/*  70 */         MetadataList meta = new MetadataList((String)obj);
/*  71 */         return new ACL(meta);
/*     */       } catch (Exception e) {
/*  73 */         Metadata meta = new Metadata((String)obj);
/*  74 */         return new ACL(meta);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private ACL get(EffectiveACLCacheKey key) throws ServiceException {
/*  80 */     return (ACL)this.mMemcachedLookup.get(key);
/*     */   }
/*     */   
/*     */   private void put(EffectiveACLCacheKey key, ACL data) throws ServiceException {
/*  84 */     this.mMemcachedLookup.put(key, data);
/*     */   }
/*     */   
/*     */   public static ACL get(String acctId, int folderId) throws ServiceException {
/*  88 */     EffectiveACLCacheKey key = new EffectiveACLCacheKey(acctId, folderId);
/*  89 */     return sTheInstance.get(key);
/*     */   }
/*     */   
/*     */   public static void put(String acctId, int folderId, ACL acl) throws ServiceException {
/*  93 */     EffectiveACLCacheKey key = new EffectiveACLCacheKey(acctId, folderId);
/*     */     
/*     */ 
/*  96 */     if (acl == null)
/*  97 */       acl = new ACL();
/*  98 */     sTheInstance.put(key, acl);
/*     */   }
/*     */   
/*     */   public void purgeMailbox(Mailbox mbox) throws ServiceException {
/* 102 */     String accountId = mbox.getAccountId();
/* 103 */     List<Folder> folders = mbox.getFolderList(null, SortBy.NONE);
/* 104 */     List<EffectiveACLCacheKey> keys = new ArrayList(folders.size());
/* 105 */     for (Folder folder : folders) {
/* 106 */       EffectiveACLCacheKey key = new EffectiveACLCacheKey(accountId, folder.getId());
/* 107 */       keys.add(key);
/*     */     }
/* 109 */     this.mMemcachedLookup.removeMulti(keys);
/*     */   }
/*     */   
/*     */   public void notifyCommittedChanges(PendingModifications mods, int changeId) {
/* 113 */     Set<EffectiveACLCacheKey> keysToInvalidate = new HashSet();
/* 114 */     if (mods.modified != null)
/* 115 */       for (Map.Entry<PendingModifications.ModificationKey, PendingModifications.Change> entry : mods.modified.entrySet()) {
/* 116 */         PendingModifications.Change change = (PendingModifications.Change)entry.getValue();
/* 117 */         Object whatChanged = change.what;
/*     */         
/*     */ 
/* 120 */         if (((whatChanged instanceof Folder)) && ((change.why & 0x200100) != 0)) {
/* 121 */           Folder folder = (Folder)whatChanged;
/*     */           
/* 123 */           acctId = folder.getMailbox().getAccountId();
/* 124 */           List<Folder> subfolders = folder.getSubfolderHierarchy();
/* 125 */           for (Folder subf : subfolders) {
/* 126 */             EffectiveACLCacheKey key = new EffectiveACLCacheKey(acctId, subf.getId());
/* 127 */             keysToInvalidate.add(key);
/*     */           }
/*     */         }
/*     */       }
/*     */     String acctId;
/* 132 */     if (mods.deleted != null)
/* 133 */       for (Map.Entry<PendingModifications.ModificationKey, PendingModifications.Change> entry : mods.deleted.entrySet()) {
/* 134 */         MailItem.Type type = (MailItem.Type)((PendingModifications.Change)entry.getValue()).what;
/* 135 */         if (type == MailItem.Type.FOLDER) {
/* 136 */           String acctId = ((PendingModifications.ModificationKey)entry.getKey()).getAccountId();
/* 137 */           if (acctId != null)
/*     */           {
/* 139 */             EffectiveACLCacheKey key = new EffectiveACLCacheKey(acctId, ((PendingModifications.ModificationKey)entry.getKey()).getItemId().intValue());
/* 140 */             keysToInvalidate.add(key);
/*     */           }
/*     */         }
/*     */       }
/*     */     try {
/* 145 */       this.mMemcachedLookup.removeMulti(keysToInvalidate);
/*     */     } catch (ServiceException e) {
/* 147 */       ZimbraLog.calendar.warn("Unable to notify folder acl cache.  Some cached data may become stale.", e);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/acl/EffectiveACLCache.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */